package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DingtalkDutyTimesDao;
import cn.topevery.garden.entity.po.DingtalkDutyTimes;
import cn.topevery.garden.entity.query.DingtalkDutyTimesQuery;
import cn.topevery.garden.entity.vo.DingtalkDutyTimesMessageVO;
import cn.topevery.garden.entity.vo.DingtalkDutyTimesVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-09-02
 */
@Repository
public class DingtalkDutyTimesDaoImpl extends BaseDaoImpl<DingtalkDutyTimes> implements DingtalkDutyTimesDao {
    @Override
    public <P extends DbCsp> List<DingtalkDutyTimes> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<DingtalkDutyTimesVO> listPage(DingtalkDutyTimesQuery query) {
        String table = "t_dingtalk_duty_times tddt";

        String columns = "tddt.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tddt.c_data_id");
        sqlQueryMate
                .where(true, "tddt.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.dataId), "tddt.c_duty_data_id", ScriptConditionEnum.Eq, query.dataId)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DingtalkDutyTimesVO.class);
    }

    @Override
    public void truncate() {
        String table = "t_dingtalk_duty_times";
        String truncateSql="truncate table ";
        run(truncateSql+table, CommandType.Text, dbCmd -> {
            dbCmd.executeNonQuery();
        });
    }

    @Override
    public List<DingtalkDutyTimesMessageVO> selectDutyTimesById(Long shiftId) {
        String table = "t_dingtalk_duty_times tddt " +
                "JOIN t_dingtalk_duty tdd ON tdd.c_data_id = tddt.c_duty_data_id ";
        String columns = "tdd.c_name dutyName,tddt.c_duty_data_id dutyDataId,tddt.c_begin `begin`,tddt.c_end `end`";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "tddt.c_duty_data_id", ScriptConditionEnum.Eq, shiftId);
        List<DingtalkDutyTimesMessageVO> list = selectList(sqlQueryMate, DingtalkDutyTimesMessageVO.class);
        return list;
    }
}
